#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
    int maxUncrossedLines(vector<int> &nums1, vector<int> &nums2) {
        int N = nums1.size();
        int M = nums2.size();
        int **dp = new int *[N + 1];
        for (int i = 0; i <= N; ++i) {
            dp[i] = new int[M + 1];
            for (int j = 0; j <= M; ++j)
                dp[i][j] = 0;
        }
        for (int i = 1; i <= N; ++i)
            for (int j = 1; j <= M; ++j)
                dp[i][j] = nums1[i - 1] != nums2[j - 1] ? max(dp[i - 1][j], dp[i][j - 1]) : dp[i - 1][j - 1] + 1;
        return dp[N][M];
    }
};

int main() {
    Solution solution;
    vector<int> a = {1, 4, 2};
    vector<int> b = {1, 2, 4};
    solution.maxUncrossedLines(a, b);
}

